package com.easylinkin.linkappapi.config.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.common.model.RequestModel;
import com.easylinkin.linkappapi.config.entity.Config;
import com.easylinkin.linkappapi.config.service.ConfigService;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.List;

/**
 * Config表控制层
 *
 * @author CodeGenerator
 * @date 2022/07/20
 */
@RestController
@RequestMapping("config")
public class ConfigController extends ApiController {
    /**
     * 服务对象
     */
    @Resource
    private ConfigService appConfigService;

    /**
     * 分页查询所有数据
     *
     * @param requestModel 查询分页对象
     * @return 所有数据
     */
    @PostMapping("getPage")
    @ApiOperation("查询分页")
    public RestMessage selectPage(@RequestBody RequestModel<Config> requestModel) {
        Assert.notNull(requestModel.getCustomQueryParams(), "customQueryParams 不能为空");
        Assert.notNull(requestModel.getPage(), "page 不能为空");
        IPage<Config> record = appConfigService.selectPage(requestModel.getPage(), requestModel.getCustomQueryParams(),false);
        return RestBuilders.successBuilder(record).build();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation("查询单条")
    public RestMessage selectOne(@PathVariable Serializable id) {
        return RestBuilders.successBuilder((this.appConfigService.getOneById(id))).build();
    }


    /**
     * 通过key查询单条数据
     *
     * @param key 关键key
     * @return 单条数据
     */
    @GetMapping("/getByKey/{key}")
    @ApiOperation("查询单条")
    public RestMessage getByKey(@PathVariable(value = "key") String key) {
        return RestBuilders.successBuilder((this.appConfigService.getOneByKey(key))).build();
    }

    /**
     * 通过key 设置配置
     *
     * @param key 关键key
     * @return 单条数据
     */
    @PostMapping("/setByKey/{key}")
    @ApiOperation("查询单条")
    public RestMessage setByKey(@PathVariable(value = "key") String key, @RequestBody Config config) {
        this.appConfigService.setByKey(key, config);
        return RestBuilders.successBuilder().build();
    }

    @PostMapping("/saveOrUpdateConfig")
    @ApiOperation("保存配置")
    @CommonOperateLogAnnotate(module = LogModule.DASHBOARD_SETTING, desc = "显示设置")
    public RestMessage saveConfig(@RequestBody Config config) {
        Assert.notNull(config,"参数不能为空");
        Assert.notNull(config.getKey(),"关键字key不能为空");
        Assert.notNull(config.getValue(),"内容不能为空");
        return RestBuilders.successBuilder((this.appConfigService.saveOrUpdateConfig(config))).build();
    }

    /**
     * 新增数据
     *
     * @param configList 实体对象
     * @return 新增结果
     */
    @PostMapping
    @ApiOperation("批量新增")
    public RestMessage insert(@RequestBody List<Config> configList) {
        this.appConfigService.saveBatch2(configList);
        return RestBuilders.successBuilder().build();
    }

    /**
     * 修改数据
     *
     * @param appConfig 实体对象
     * @return 修改结果
     */
    @PutMapping
    @ApiOperation("修改单条")
    public RestMessage update(@RequestBody Config appConfig) {
        return RestBuilders.successBuilder().success(this.appConfigService.updateOne(appConfig)).build();
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping
    @ApiOperation("批量删除")
    public RestMessage delete(@RequestParam("idList") List<Long> idList) {
        return RestBuilders.successBuilder().success(this.appConfigService.deleteByIds(idList)).build();
    }

    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody Config appConfig, HttpServletRequest request, HttpServletResponse response) {
        appConfigService.export(appConfig, request, response);
    }

    /**
     * 获取eduUrl
     * @return
     */
    @GetMapping("/getEduUrl")
    @ApiOperation("查询单条")
    public RestMessage getEduUrl() {
        return RestBuilders.successBuilder().data(this.appConfigService.getEduUrl()).build();
    }
}

